package com.example.hospitalsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.hospitalsystem.mapper.DoctorMapper;
import com.example.hospitalsystem.mapper.UserMapper;
import com.example.hospitalsystem.pojo.dto.user.*;
import com.example.hospitalsystem.pojo.entity.Doctor;
import com.example.hospitalsystem.pojo.entity.User;
import com.example.hospitalsystem.result.Result;
import com.example.hospitalsystem.service.UserService;
import com.example.hospitalsystem.utils.AliOssUtil;
import com.example.hospitalsystem.utils.JwtHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/user")
@Tag(name = "2.用户模块")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private AliOssUtil aliOssUtil;

    @Autowired
    UserMapper userMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Operation(summary = "管理员获取所有普通用户信息",security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/getAllNormelUsers")
    public Result<Object> getAllNormelUserInfo(@RequestBody UserPageQueryDto userPageQueryDto){
        return userService.getAllNomerlUserInfo(userPageQueryDto);
    }

    @Operation(summary = "管理员用户个人信息",security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/getUserInfo")
    public Result<Object> getUserInfo(){
        return userService.getUserInfo();
    }

    @Operation(summary = "管理员删除用户信息",security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/deleteUserInfo/{id}")
    public Result<Object> deleteUserInfo(@PathVariable("id") Integer id){
        return userService.deleteUserInfo(id);
    }

    @Operation(summary = "管理员编辑用户信息",security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/editUserInfo")
    public Result<Object> editUserInfo(@RequestBody EditDto editDto){
        return userService.editUserInfo(editDto);
    }

    @Operation(summary = "管理员获取所有管理员信息",security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/getAllAdmin")
    public Result<Object> getAllAdmin(@RequestBody AdminPageQueryDto adminPageQueryDto){
        return userService.getAllAdmin(adminPageQueryDto);
    }



    @Operation(summary = "管理员编辑管理员信息",security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/editAdminInfo")
    public Result<Object> editAdminInfo(@RequestBody EditDto editDto){
        return userService.editAdminInfo(editDto);
    }

    @Operation(summary = "管理员删除管理员信息",security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/deleteAdminInfo/{id}")
    public Result<Object> deleteAdminInfo(@PathVariable("id") Integer id){
        return userService.deleteAdminInfo(id);
    }

    @Operation(summary = "根据用户名查找角色信息")
    @PostMapping("/getRoleByUn")
    public Result getRoleByUn(@RequestBody LookRoleByUnDto lookRoleByUnDto){
        return userService.getRoleByUserName(lookRoleByUnDto);
    }

    @Operation(summary = "根据邮箱查找角色信息")
    @PostMapping("/getRoleByEmail")
    public Result getRoleByEmail(@RequestBody LookRoleByEmail lookRoleByUnDto){
        return userService.getRoleByEmail(lookRoleByUnDto);
    }

    @Operation(summary = "管理员发布通告",security = {@SecurityRequirement(name = "Authorization")})
    @PutMapping("/pushNotice")
    public Result<Object> pushNotice(@RequestBody NoticeDto noticeDto){
        return userService.pushNotice(noticeDto);
    }

    @Operation(summary = "上传头像",security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/uploadImage")
    public Result uploadImage(@RequestParam MultipartFile image) throws Exception{

        String token=request.getHeader("Authorization");
        Integer userId=jwtHelper.getUserId(token);
        String url=aliOssUtil.upload(image);
        User user=new User();
        user.setImage(url);
        LambdaUpdateWrapper<User> userLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        userLambdaUpdateWrapper.eq(User::getUserId,userId);

        User doctorUser=userMapper.selectOne(userLambdaUpdateWrapper);
        if(doctorUser.getRole()==3){
            LambdaUpdateWrapper<Doctor> doctorLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
            doctorLambdaUpdateWrapper.eq(Doctor::getUserId,userId);
            Doctor doctor=doctorMapper.selectOne(doctorLambdaUpdateWrapper);
            doctor.setDoctorPhoto(url);
            int i=doctorMapper.update(doctor,doctorLambdaUpdateWrapper);
        }


        int i=userMapper.update(user,userLambdaUpdateWrapper);
        if(i>0){
            return Result.build(url,200,"添加照片信息成功");

        }
        return Result.build(200,"添加照片信息失败");

    }

    @Operation(summary = "获取头像信息",security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/getImage")
    public Result getImageInfo(){
        return userService.getImage();
    }

    @Operation(summary = "搜索用户", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/search")
    public Result<Object> searchUser(String key){
        List<User> list = userService.searchUser(key);
        return Result.ok(list);
    }

    @Operation(summary = "修个个人介绍", security = {@SecurityRequirement(name = "Authorization")})
    @PutMapping("/changeUser")
    public Result changeUser(@RequestBody User user){
        return userService.changeUserInfo(user);
    }

}

